const express = require('express')
const router = express.Router()
const { insertFeedback, getReadMsg, getReadList, getReadListCount, modifyReadIt } = require('../../model/mongo')

router.get('/readlist', async (req, res) => {
    const { userid } = req.query
    const result = {
        code: 200,
        msg: '获取已读消息'
    }
    result.data = await getReadList(userid)
    res.send(JSON.stringify(result))
})

router.get('/unreadlist', async (req, res) => {
    const { userid } = req.query
    const result = {
        code: 200,
        msg: '获取未读消息'
    }
    result.data = await getReadMsg(userid)
    res.send(JSON.stringify(result))
})

router.get('/count', async (req, res) => {
    const { userid } = req.query
    const result = {
        code: 200,
        msg: '获取未读消息总数'
    }
    result.count = await getReadListCount(userid)
    res.send(JSON.stringify(result))
})

router.post('/feedback', async (req, res) => {
    const { userid, msg } = req.body
    const result = {
        code: 200,
        msg: '用户反馈问题'
    }
    result.msg = await insertFeedback(userid, msg)
    res.send(JSON.stringify(result))
})

router.post('/readit', async (req, res) => {
    const { msgid, userid } = req.body
    const result = {
        code: 200,
        msg: '标记为已读'
    }
    result.data = await modifyReadIt(msgid, userid)
    res.send(JSON.stringify(result))
})

module.exports = router
